package com.xgpro.mobile.talentintroduction.controller;


import com.xgpro.common.controller.BaseController;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.common.utils.FileTool;
import com.xgpro.common.utils.MailTool;
import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.domain.rolemanage.entity.Department;
import com.xgpro.domain.talentintroduction.entity.Attachment;
import com.xgpro.domain.talentintroduction.entity.IntroduceJob;
import com.xgpro.domain.talentintroduction.entity.Recruitment;
import com.xgpro.domain.talentintroduction.entity.User;
import com.xgpro.domain.talentintroduction.vo.AttachmentFront;
import com.xgpro.domain.talentintroduction.vo.RecruitmentBack;
import com.xgpro.domain.talentintroduction.vo.RecruitmentFront;
import com.xgpro.mapper.rolemanage.DepartmentMapper;
import com.xgpro.mapper.talentintroduction.RecruitmentMapper;
import com.xgpro.mapper.talentintroduction.UserMapper;
import com.xgpro.mobile.talentintroduction.service.RecruitmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/18 20:15
 */
@RestController
@RequestMapping("/recruitment")
public class RecruitmentController extends BaseController {
    private static final String DIR= ReadPropertiesUtil.readUrl("re_dir");
    /**
     * 测试session存值
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/test")
    public void setSession(HttpSession httpSession){
            httpSession.setAttribute("id",260);
            httpSession.setAttribute("deptId",1);
            httpSession.setAttribute("secondDeptId",1);
            List<Integer> roleIds= new ArrayList<>();
            roleIds.add(1);
            roleIds.add(2);
            roleIds.add(3);
            httpSession.setAttribute("roleIds",roleIds);
    }

    @Autowired
    private RecruitmentService recruitmentService;

    /**
     * 招聘信息一览
     * @param recruitmentFront
     * @return
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/qryRecruitmentList")
    public ResponseResult qryRecruitmentList(@RequestBody RecruitmentFront recruitmentFront) {
        try{
            return ResponseResult.ok(recruitmentService.qryRecruitmentList(recruitmentFront));
        }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.NO_RECRUITMENT.getCode(),ResultCodeEnum.NO_RECRUITMENT.getMessage());
        }
    }

    /**
     * 招聘信息详情
     * @param id
     * @return
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/qryRecruitmentById")
    public ResponseResult qryRecruitmentById(Integer id){
        try {
            return ResponseResult.ok(recruitmentService.qryRecruitmentById(id));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 人才引荐信息回显
     * @param httpSession
     * @param recruitmentId
     * @return
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/qryUserInfoByRecruitmentId")
    public ResponseResult qryUserInfoByRecruitmentId(HttpSession httpSession,@RequestParam Integer recruitmentId){
        try{
            Integer userId =(Integer) httpSession.getAttribute("id");
            if(userId==null){
//                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
                return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(recruitmentService.qryUserInfoByRecruitmentId(recruitmentId,userId));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    @Resource
    private UserMapper userMapper;

    @Resource
    private RecruitmentMapper recruitmentMapper;

    @Resource
    private DepartmentMapper departmentMapper;
    /**
     * 提交应聘信息
     * @param httpSession
     * @param introduceJob
     * @return
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/addIntroduceJob")
    public ResponseResult addIntroduceJob(HttpSession httpSession, IntroduceJob introduceJob){
        try{
            Integer userId =(Integer) httpSession.getAttribute("id");
            if(userId==null||"".equals(userId)){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            if(introduceJob.getMultipartFile()==null||introduceJob.getMultipartFile().isEmpty()){
                throw new BusinessRuntimeException(ResultCodeEnum.UPLOAD_ISEMPTY.getCode(),ResultCodeEnum.UPLOAD_ISEMPTY.getMessage());
            }
            Recruitment recruitment = recruitmentMapper.selectByPrimaryKey(introduceJob.getRecruitmentId());
            Integer deptId = recruitment.getDeptId();
            Department department = departmentMapper.selectByPrimaryKey(deptId);
            String deptName = department.getName();
            String url = FileTool.upLoad(introduceJob.getMultipartFile(), DIR);
            introduceJob.setAttachmentAddress(url);
            String filePath = url.replace(ReadPropertiesUtil.readUrl("nginxUrl"),ReadPropertiesUtil.readUrl("upload_path"));
            System.out.println(filePath);
            if(introduceJob.getAttachmentAddress()!=null&&!"".equals(introduceJob.getAttachmentAddress())){
                MailTool.sendMail(ReadPropertiesUtil.readUrl("xg_mail"),"人才引荐-"+deptName+"-"+introduceJob.getName(),"",filePath);
            }
            recruitmentService.addIntroduceJob(introduceJob,userId);
            return ResponseResult.ok("提交成功");
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.UPLOAD_FALE.getCode(),ResultCodeEnum.UPLOAD_FALE.getMessage());
        }
    }

    /**
     * 上传简历
     * @param multipartFile
     * @return
     */
    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/uploadRecAttach")
    public ResponseResult uploadRecAttach(MultipartFile multipartFile){
        try{
            if(multipartFile==null||multipartFile.isEmpty()){
                throw new BusinessRuntimeException(ResultCodeEnum.UPLOAD_ISEMPTY.getCode(),ResultCodeEnum.UPLOAD_ISEMPTY.getMessage());
            }
            String url = FileTool.upLoad(multipartFile, DIR);
            return ResponseResult.ok(url);
        }catch (BusinessRuntimeException be){
            //be.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.UPLOAD_ISEMPTY.getCode(),ResultCodeEnum.UPLOAD_ISEMPTY.getMessage());
        }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.UPLOAD_FALE.getCode(),ResultCodeEnum.UPLOAD_FALE.getMessage());
        }
    }

}
